//UserInput.cpp
// compile with: /EHsc /LD
#include "Including.h"

using namespace std;


namespace SDLFuncs
{
	//keyboard events
	SDL_Event event;

	////////////////////////
	/* Input handling 
	*/
	int MySDLFuncs::Input(void *data)
	{
		//Lock
		SDL_SemWait( threadLock );

		if(SDL_PollEvent(&event))
		{
			switch(event.type)
			{
				case SDL_KEYDOWN:
					switch(event.key.keysym.sym)
					{
						//Play the Music
						case SDLK_9:
							//If there is no music playing
							if( Mix_PlayingMusic() == 0 ){
								//Play the music
								Mix_PlayMusic( music, -1 );
							}
							//If music is being played
							else{
								//If the music is paused
								if( Mix_PausedMusic() == 1 ){
									//Resume the music
									Mix_ResumeMusic();
								}
								//If the music is playing
								else{
									//Pause the music
									Mix_PauseMusic();
								}
							}
							break;
						//Stop the Music
						case SDLK_0:
							Mix_HaltMusic();
							break;

						//ESCAPE
						case SDLK_ESCAPE:
							quit = true;
							break;
						//DEFAULT
						default:
						break;
					}
					break;
				//EXIT SYSTEM
				case SDL_QUIT:
					quit = true;
					break;
			}
		}
		


		//Camera Conrolling		
		Uint8 *keystates = SDL_GetKeyState(NULL);
		
		rad = float(PI*s_angle/180.0f);
		if(keystates[SDLK_UP])
		{	
			s_eye[2] += (float)sin(rad) * speed; 
			s_eye[0] += (float)cos(rad) * speed;  
		}
		if(keystates[SDLK_DOWN])
		{	
			s_eye[2] -= (float)sin(rad) * speed; 
			s_eye[0] -= (float)cos(rad) * speed; 
		}
		if(keystates[SDLK_RIGHT])
			s_angle += 0.1;
		if(keystates[SDLK_LEFT])
			s_angle -= 0.1;
		if(keystates[SDLK_r])
			y_angle += 0.1;
		if(keystates[SDLK_f])
			y_angle -= 0.1;


		//Character Moving
		if((keystates[SDLK_a])||(keystates[SDLK_d])||(keystates[SDLK_w])||(keystates[SDLK_s])||(keystates[SDLK_SPACE])) {
			if(keystates[SDLK_a])
			{	x_force += 0.02;
				myModel->SetState(MODEL_RUN);
			}
			if(keystates[SDLK_d])
			{	x_force -= 0.02;
				myModel->SetState(MODEL_RUN);
			}
			if(keystates[SDLK_w])
			{	z_force += 0.02;
				myModel->SetState(MODEL_RUN);
			}
			if(keystates[SDLK_s])
			{	z_force -= 0.02;
				myModel->SetState(MODEL_RUN);
			}
			if(keystates[SDLK_SPACE])
			{	y_force -= 0.1f; 
				myModel->SetState(MODEL_JUMP);
			}
		}else
		{
			myModel->SetState(MODEL_IDLE);
		}

		//Unlock
		SDL_SemPost( threadLock );

		return 0;
	}
	
}